/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mattespill.gui;

/**
 *
 * @author auchaunn
 */
    
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import mattespill.*;


class GUILoggInn extends JPanel{
    
    // opprette textfelt og passordfelt
    private JTextField brukernavnFelt   = new JTextField(20);
    private JLabel label_brukernavn     = new JLabel ("Brukernavn:");
    private JTextField passordFelt      = new JPasswordField(20);
    private JLabel label_passord        = new JLabel ("Passord:");
    private Label meldingsFelt          = new Label("");
    private Oversikt oversikt           = new Oversikt();
    private JPanel panelBgImg           = new JPanel();
    private BufferedImage bg;
    private BufferedImage bi;
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
   private Image bak                    = new ImageIcon("src/mattespill/gui/images/bakgrunn.png").getImage();
        
    
  
		
	
    private Color c                     = new Color(255,255,255, 150);
    private Color t                     = new Color(255,255,255, 0);
    private JPanel logginn              = new JPanel();
    private JButton knapp               = new JButton("Logg inn");
   // Image bg = new ImageIcon("src/mattespill/gui/images/bakgrunn.png").getImage();
    
    //Vindu
    public GUILoggInn(){
 
        //bg = imageToBufferedImage(bak);
             
        this.oversikt = new Oversikt();
//        setTitle(tittel);
  //      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setMinimumSize(new Dimension(768,512));
        
//        setLocationRelativeTo(null);
        //setLayout(new FlowLayout());
        setLayout(new BorderLayout());
        logginn.add(new sammen(), BorderLayout.CENTER);
        logginn.add(new knapp(), BorderLayout.EAST);
        logginn.setBackground(c);
        add(logginn);        
    }
    
    private class sammen extends JPanel{
        public sammen(){
            //setBackground(t);
            add(new panel_label());
            add(new panel_felt());
       }
    }
    
    private class panel_label extends JPanel {
        public panel_label(){
            setLayout(new GridLayout(2,1,14,13));
            setBackground(t);
            //label_brukernavn.setForeground(Color.WHITE);
            //label_passord.setForeground(Color.WHITE);
            add(label_brukernavn);
            add(label_passord);
            
        
        }
       
    }
    private class panel_felt extends JPanel {
        public panel_felt(){
            setLayout(new GridLayout(2,1));
            setBackground(t);
            add(brukernavnFelt).addKeyListener(new KoyListener());
            add(passordFelt).addKeyListener(new KoyListener());;
        }
    }
    private class knapp extends JPanel{
        public knapp(){
            Knappelytter knappelytteren = new Knappelytter(this);
            knapp = new JButton("Logg inn");
            setBackground(t);
            add(knapp);
            knapp.addActionListener(knappelytteren);

        }
    }
    private class KoyListener implements KeyListener {

        public void keyPressed(KeyEvent e) {
            int a = e.getKeyCode();

            if(a == e.VK_ENTER){
                String navn = brukernavnFelt.getText();
                 String pass = passordFelt.getText();
                 System.out.println(oversikt.loggInn(navn,pass));
                 if(oversikt.loggInn(navn,pass)){
                     if(oversikt.getBruker().getRolle() == 1){
                        GUIAdminpanel g = new GUIAdminpanel(oversikt);
                        JPanel mainPanel = (JPanel)getParent();
                        mainPanel.removeAll();// dette is the sub-panel
                        mainPanel.repaint();
                        mainPanel.add(g);
                        mainPanel.revalidate();
                        g.repaint();
                     }else{
                        GUIFortsett g = new GUIFortsett(oversikt);
                        JPanel mainPanel = (JPanel)getParent();
                        mainPanel.removeAll();// dette is the sub-panel
                        mainPanel.repaint();
                        mainPanel.add(g);
                        mainPanel.revalidate();
                        g.repaint();
                     }

                 }
                 else{
                     JOptionPane.showMessageDialog(null,"Feil brukernavn og/eller passord!");
                 }
            }

        }


        public void keyTyped(KeyEvent e) {

            
        }

        public void keyReleased(KeyEvent e) {
           
        }
    }
    private class Knappelytter implements ActionListener{
        private JPanel dette;

        public Knappelytter(JPanel dette){
            this.dette = dette;
        }
        public void actionPerformed(ActionEvent hendelse){
            
             String navn = brukernavnFelt.getText();
             String pass = passordFelt.getText();
             if(oversikt.loggInn(navn,pass)){
                 if(oversikt.getBruker().getRolle() == 1){
                    GUIAdminpanel g = new GUIAdminpanel(oversikt);
                    JPanel mainPanel = (JPanel)getParent();
                    mainPanel.removeAll();// dette is the sub-panel
                    mainPanel.repaint();
                    mainPanel.add(g);
                    mainPanel.revalidate();
                    g.repaint();
                 }else{
                    GUIFortsett g = new GUIFortsett(oversikt);
                    JPanel mainPanel = (JPanel)getParent();
                    mainPanel.removeAll();// dette is the sub-panel
                    mainPanel.repaint();
                    mainPanel.add(g);
                    mainPanel.revalidate();
                    g.repaint();
                 }
                 
             }
             
             else{
                 JOptionPane.showMessageDialog(null,"Feil brukernavn og/eller passord!");
             }
            
        }
    }
    
    
    /*
    private class InnloggingsPanel extends JPanel{
        public InnloggingsPanel(){
            setLayout(new GridLayout(2,2,2,2));
            
            add(brukernavn);
            add(brukernavnFelt);
            brukernavnFelt.setInputVerifier(new BrukernavnKontroll());
            
            add(passord);
            add(passordFelt);
            passordFelt.setInputVerifier(new PassordKontroll());
        }
    }
    
    class BrukernavnKontroll extends InputVerifier{
        
        
    }

    */
    public static BufferedImage imageToBufferedImage(Image im) {
     BufferedImage bi = new BufferedImage
        (im.getWidth(null),im.getHeight(null),BufferedImage.TYPE_INT_RGB);
     Graphics bg = bi.getGraphics();
     bg.drawImage(im, 0, 0, null);
     bg.dispose();
     return bi;
  }
    
  
}


